<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== itellico Net Architect ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    *
    *********************************************************/

    /***
    * Abstract Class installNodeInserat
    ****
    * This is the module installer/deinstaller, shall
    * automate integration of new modules into client
    * solutions
    ****
    * @package installNodes
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    ***/
    class installNodeShopArticle extends installNode{

        /**
         * This is the node install script
         *
         */
        protected function create(){
            $this->db->Execute("CREATE TABLE shop_notifications(
                                        id          SERIAL,
                                        name        VARCHAR(255),
                                        codeset     VARCHAR(255),

                                        PRIMARY KEY(id),
                                        UNIQUE(codeset)
                                    )");

            $this->db->Execute("insert into shop_notifications(name, codeset) values('E-Mail','email')");

            //create database table
            $this->db->Execute("CREATE TABLE shop_suppliers(
                                    id              SERIAL,

                                    name            VARCHAR(255),
                                    adr1            VARCHAR(255),
                                    adr2            VARCHAR(255),
                                    city            VARCHAR(100),
                                    country         VARCHAR(100),
                                    zip             VARCHAR(12),
                                    tel             VARCHAR(32),
                                    fax             VARCHAR(32),
                                    mail            VARCHAR(100),
                                    notes           VARCHAR(255),

                                    p1name          VARCHAR(100),
                                    p1pos           VARCHAR(100),
                                    p1notes         VARCHAR(255),

                                    p2name          VARCHAR(100),
                                    p2pos           VARCHAR(100),
                                    p2notes         VARCHAR(255),

                                    p3name          VARCHAR(100),
                                    p3pos           VARCHAR(100),
                                    p3notes         VARCHAR(255),

                                    notification    INTEGER,

                                    PRIMARY KEY(id),

                                    FOREIGN KEY(notification) REFERENCES shop_notifications ON DELETE SET NULL ON UPDATE CASCADE
                                )");

            $this->db->Execute("CREATE TABLE node_shop_article(
                                    id              SERIAL,
                                    content         TEXT,
                                    teaser          TEXT,

                                    teaser_title    VARCHAR(255),
                                    banner_title    VARCHAR(100),
                                    banner_desc     VARCHAR(255),

                                    image           BOOL DEFAULT FALSE,

                                    supplier        INTEGER,
                                    supplier_aid    VARCHAR(255),

                                    price           FLOAT DEFAULT 0,
                                    tax             FLOAT DEFAULT 0.2,
                                    currency        VARCHAR(3) DEFAULT 'EUR',
                                    parity          FLOAT DEFAULT 1,

                                    order_unit      VARCHAR(32) DEFAULT 'STK',
                                    content_unit    VARCHAR(32) DEFAULT 'STK',
                                    co_co_per_ou    INTEGER DEFAULT 1,

                                    PRIMARY KEY(id),
                                    FOREIGN KEY(supplier) REFERENCES shop_suppliers ON DELETE SET NULL ON UPDATE CASCADE
                                )");

            //$this->db->Execute("CREATE INDEX node_shop_article_on_splash ON node_shop_article(on_splash)");
            //$this->db->Execute("CREATE INDEX node_shop_article_on_package ON node_shop_article(on_package)");

            $this->db->Execute("CREATE TABLE node_shop_order(
                                    id              SERIAL,

                                    name            VARCHAR(255),
                                    tel             VARCHAR(100),
                                    mail            VARCHAR(100),

                                    price           FLOAT DEFAULT 0,
                                    tax             FLOAT DEFAULT 0.2,
                                    currency        VARCHAR(3) DEFAULT 'EUR',
                                    parity          FLOAT DEFAULT 1,

                                    notified        BOOLEAN DEFAULT FALSE,
                                    payed           BOOLEAN DEFAULT FALSE,

                                    tid             VARCHAR(255),
                                    t_status        VARCHAR(20),
                                    t_intime        TIMESTAMP,
                                    t_price         INTEGER,
                                    t_currency      VARCHAR(4),
                                    t_p_type        VARCHAR(10),
                                    t_brand         VARCHAR(32),
                                    t_ship_adr      VARCHAR(100),
                                    t_bill_adr      VARCHAR(100),
                                    t_orderdesc     VARCHAR(100),
                                    t_customer      VARCHAR(100),

                                    created         TIMESTAMP DEFAULT NOW(),
                                    PRIMARY KEY(id)
                                )");
            $this->db->Execute("ALTER SEQUENCE node_shop_order_id_seq RESTART WITH 15000");

            $this->db->Execute("CREATE TABLE node_shop_order_article(
                                    article_id      INTEGER,
                                    quant1          INTEGER,
                                    price           FLOAT DEFAULT 0,
                                    tax             FLOAT DEFAULT 0.2,
                                    currency        VARCHAR(3) DEFAULT 'EUR',
                                    parity          FLOAT DEFAULT 1,
                                    order_id        INTEGER,

                                    PRIMARY KEY(article_id, order_id),
                                    FOREIGN KEY(order_id) REFERENCES node_shop_order ON DELETE CASCADE ON UPDATE CASCADE
                                )");

            $this->db->Execute("CREATE INDEX node_shop_article_supplier_aid ON node_shop_article(supplier_aid)");
            $this->db->Execute("insert into user_cp_access(name, codeset, istab) values('Suppliers', 'suppliers', TRUE)");
            $this->db->Execute("insert into user_cp_access(name, codeset, istab) values('Reports', 'shopreports', TRUE)");
        }


        /**
         * This is the node uninstall script
         *
         */
        protected function remove(){
            //drop database table
            $this->db->Execute("DROP TABLE node_shop_article");
            $this->db->Execute("DROP TABLE shop_suppliers");
            $this->db->Execute("DROP TABLE shop_notifications");
            $this->db->Execute("DROP TABLE node_shop_order_article");
            $this->db->Execute("DROP TABLE node_shop_order");
            $this->db->Execute("DELETE FROM user_cp_access WHERE codeset = 'suppliers'");
            $this->db->Execute("DELETE FROM user_cp_access WHERE codeset = 'shopreports'");
        }


        /**
         * Sets the properties of this node
         *
         */
        protected function setProps(){
            $this->setDescription('Shop Article');
            $this->setAdminAdd(true);
        }
    }
?>